/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "newspromptdialog.h"

#include <QDialog>

#include <QVBoxLayout>
#include <QWebView>
#include <QFrame>
#include <QHBoxLayout>
#include <QCheckBox>
#include <QLabel>
#include <QPushButton>

#include <QDebug>

#include "../Types/news.h"
#include "globals.h"

NewsPromptDialog :: NewsPromptDialog(const QList<News>& newsList, const bool isStartup, QWidget* parent/*=0*/)
    :QDialog(parent),newsList(newsList),currentNews(0)
{
    this->setWindowTitle(tr("NUP : News"));

    pMainGrid = new QVBoxLayout(this);
    pNewsViewer = new QWebView(this);
    pNewsViewer->setHtml(QString("<h2>") + newsList[0].title + QString("</h2><h5>") + newsList[0].date + QString("</h5><hr />") + newsList[0].text,QUrl(DEFAULT_SERVER));

    // It's a HLine
    pSepLine = new QFrame(this);
    pSepLine->setFrameStyle(QFrame::HLine);
    pSepLine->setLineWidth(1);

    pButtonGrid = new QHBoxLayout();
    pCheckBoxGrid = new QHBoxLayout();
    pTitleManagementGrid = new QHBoxLayout();
    pArrowsGrid = new QHBoxLayout();

    pPopupSetCBox = new QCheckBox(tr("Display at startup"),this);
    pPopupSetCBox->setChecked(isStartup);

    pSepButtonLine = new QFrame(this);
    pSepButtonLine->setFrameStyle(QFrame::VLine);
    pSepButtonLine->setLineWidth(1);

    pPreviousTitleLabel = new QLabel("",this);
    pPreviousTitleLabel->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Maximum);
    pPreviousButton = new QPushButton("<",this);
    pPreviousButton->setEnabled(false); // We are starting from 0 so we can't go behind
    pPreviousButton->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);
    pPreviousButton->setMaximumWidth(20);

    pNextButton = new QPushButton(">",this);
    pNextButton->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);
    pNextButton->setMaximumWidth(20);
    if ( newsList.size() != 0 )
    {
        pNextTitleLabel = new QLabel(newsList[1].title,this);
    }
    else
    {
        pNextTitleLabel = new QLabel("",this);
        pNextButton->setEnabled(false);
    }
    pNextTitleLabel->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Maximum);
    pCloseButton = new QPushButton("X",this);
    pCloseButton->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);
    pCloseButton->setMaximumWidth(20);

    pMainGrid->addWidget(pNewsViewer);
    pMainGrid->addWidget(pSepLine);
    pMainGrid->addLayout(pButtonGrid);
    pButtonGrid->addLayout(pCheckBoxGrid);
    pCheckBoxGrid->addWidget(pPopupSetCBox,0,Qt::AlignLeft);
    pCheckBoxGrid->addWidget(pSepButtonLine,0,Qt::AlignLeft);
    pButtonGrid->addLayout(pTitleManagementGrid);
    pTitleManagementGrid->addWidget(pPreviousTitleLabel);
    pTitleManagementGrid->addLayout(pArrowsGrid);
    pArrowsGrid->addWidget(pPreviousButton);
    pArrowsGrid->addWidget(pNextButton);
    pTitleManagementGrid->addWidget(pNextTitleLabel);
    pButtonGrid->addWidget(pCloseButton,0,Qt::AlignRight);

    connect(pPreviousButton, SIGNAL(clicked()), this, SLOT(previousNews()));
    connect(pNextButton, SIGNAL(clicked()), this, SLOT(nextNews()));
    connect(pCloseButton, SIGNAL(clicked()), this, SLOT(accept()));

    qDebug() << "NewsPromptDialog created";
}

NewsPromptDialog :: ~NewsPromptDialog(void)
{
    qDebug() << "NewsPromptDialog deleted";
}

void NewsPromptDialog :: previousNews()
{
    qDebug() << "NewsPromptDialog :: previousNews (Current: " << currentNews << " NbNews: " << newsList.size() << ")";

    pNextButton->setEnabled(true);
    pNextTitleLabel->setText(newsList[currentNews].title);

    currentNews--;

    if ( currentNews == 0 )
    {
        pPreviousButton->setEnabled(false);
        pPreviousTitleLabel->setText("");
    }
    else
    {
        pPreviousTitleLabel->setText(newsList[currentNews-1].title);
    }

    pNewsViewer->setHtml(QString("<h2>") + newsList[currentNews].title + QString("</h2><h5>") + newsList[currentNews].date + QString("</h5><hr />") + newsList[currentNews].text,QUrl(DEFAULT_SERVER));
}

void NewsPromptDialog :: nextNews()
{
    qDebug() << "NewsPromptDialog :: nextNews (Current: " << currentNews << " NbNews: " << newsList.size() << ")";

    pPreviousButton->setEnabled(true);
    pPreviousTitleLabel->setText(newsList[currentNews].title);

    currentNews++;

    if ( currentNews == (unsigned int)newsList.size()-1 )
    {
        pNextButton->setEnabled(false);
        pNextTitleLabel->setText("");
    }
    else
    {
        pNextTitleLabel->setText(newsList[currentNews+1].title);
    }

    pNewsViewer->setHtml(QString("<h2>") + newsList[currentNews].title + QString("</h2><h5>") + newsList[currentNews].date + QString("</h5><hr />") + newsList[currentNews].text,QUrl(DEFAULT_SERVER));
}


void NewsPromptDialog :: accept(void)
{
    qDebug() << "NewsPromptDialog :: accept";

    this->done(QDialog::Accepted);
    emit deleteNPDSignal();
}

void NewsPromptDialog :: reject(void)
{
    qDebug() << "NewsPromptDialog :: reject";

    this->done(QDialog::Rejected);
    emit deleteNPDSignal();
}


bool NewsPromptDialog :: hasNewsAtStartup(void)const
{
    qDebug() << "NewsPromptDialog :: hasNewsAtStartup";

    return pPopupSetCBox->isChecked();
}
